package com.zjx.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjx.reggie.common.R;
import com.zjx.reggie.dto.SetmealDto;
import com.zjx.reggie.entity.Category;
import com.zjx.reggie.entity.Setmeal;
import com.zjx.reggie.service.CategoryService;
import com.zjx.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@RequestMapping("/setmeal")
@RestController
public class SetmealController {

    @Resource
    private SetmealService setmealService;

    @Resource
    private CategoryService categoryService;

    @GetMapping("/page")
    public R page(Long page ,Long pageSize,String name){
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>(page,pageSize);

        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);

        setmealService.page(setmealPage,qw);

        BeanUtils.copyProperties(setmealPage,setmealDtoPage);

        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> list  = records.stream().map((item) -> {

            SetmealDto setmealDto = new SetmealDto();

            BeanUtils.copyProperties(item,setmealDto);

            String id = item.getCategoryId();
            Category category = categoryService.getById(id);

            setmealDto.setCategoryName(category.getName());

            return setmealDto;

        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(list);

        return R.success(setmealDtoPage);
    }

    @PostMapping()
    public R save(@RequestBody SetmealDto setmealDto){

        setmealService.saveSetmealAndDish(setmealDto);

        return R.success("套餐添加成功！");
    }

    @PostMapping("/status/0")
    public R discontinued(@RequestParam("ids") List<String> ids){
        log.info(ids.toString());

        LambdaUpdateWrapper<Setmeal> qw = new LambdaUpdateWrapper<>();
        qw.in(Setmeal::getId,ids);
        qw.set(Setmeal::getStatus,0);

        setmealService.update(qw);
        return R.success("商品已停售！");
    }

    @PostMapping("/status/1")
    public R sales(@RequestParam("ids") List<String> ids){

        LambdaUpdateWrapper<Setmeal> qw = new LambdaUpdateWrapper<>();
        qw.in(Setmeal::getId,ids);
        qw.set(Setmeal::getStatus,1);
        setmealService.update(qw);


        return R.success("商品启售成功！");
    }

    @DeleteMapping()
    public R delete(@RequestParam("ids") List<String> ids){

        LambdaUpdateWrapper<Setmeal> qw = new LambdaUpdateWrapper<>();
        qw.in(Setmeal::getId,ids);

        setmealService.remove(qw);
        return R.success("删除成功！");

    }

    @GetMapping("/{id}")
    public R getById(@PathVariable String id){
        return R.success(setmealService.getByIdSetmeal(id));
    }

    @PutMapping()
    public R update(@RequestBody SetmealDto setmealDto){
        setmealService.updateByIdSetmealDish(setmealDto);
        return R.success("修改成功！");
    }

    @GetMapping("/list")
    public R list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        qw.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());

        List<Setmeal> list = setmealService.list(qw);
        return R.success(list);
    }

    @GetMapping("/dish/{id}")
    public R dish(@PathVariable String id){

        Setmeal setmeal = setmealService.getById(id);
        return R.success(setmeal);
    }
}
